﻿using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace integrationServer
{
    class HttpProcessor
    {
        private TcpClient s = null;
        private StreamReader sr = null;
        private StreamWriter sw = null;

        private String method = "";
        private String url = "";
        private String protocol = "";
        private String trame = "";
        private String[] body = null;

        protected String response200 = "HTTP/1.0 200 OK";
        protected String response201 = "HTTP/1.0 201 Created & Location";
        protected String response204 = "HTTP/1.0 204 Created & No Content";
        protected String response401 = "HTTP/1.0 401 Unauth";
        protected String response403 = "HTTP/1.0 403 Forbid. (rsc exist)";
        protected String response404 = "HTTP/1.0 404 Not Found";
        protected String response415 = "HTTP/1.0 415 Unsupported Media Type";

        public HttpProcessor(TcpClient s)
        {
            this.s = s;
        }

        public void process()
        {
            NetworkStream ns = this.s.GetStream(); 
            
            this.sr = new StreamReader(ns);
            this.sw = new StreamWriter(ns);
            
            this.readRequest();
            this.s.Close();
        }

        // Read the client request 
        public void readRequest()
        {
            String request = this.sr.ReadLine();
            String[] tokens = request.Split(new char[] { ' ' });

            // Set vaue for my request attribute
            this.method = tokens[0];
            this.url = tokens[1];
            this.protocol = tokens[2];

            // Read the stream
            while (this.sr.Peek() >= 0)
            {
                this.trame += (char)this.sr.Read();
            }

            Console.WriteLine("METHOD :: "+this.method);
            /*  
                String line = "";
                while ((line = this.sr.ReadLine()) != null)
                {
                    String[] tokens = line.Split(new char[] { ':' });
                    String name = tokens[0];
                    String value = "";

                    for (int i=1; i<tokens.Length; i++)
                    {
                        value += tokens[i];
                        if (i < tokens.Length - 1) tokens[i] += ":";
                    }

                    this.hashTable.Add(name,value);

                    //Console.WriteLine(line);
                }

                // Research in the hashtable the key content-length
                object obj = this.hashTable["Content-Length"];

                // Cast object to int 
                this.contentLength = Convert.ToInt16(obj);
            */

            if (this.method == "POST")
            {
                // Get the body
                this.body = this.trame.Split(new String[] { "\r\n\r\n" }, StringSplitOptions.None);
                this.managePost();
            }
                else if (this.method == "GET")
                {
                    this.manageGet();
                }
                    else if (this.method == "DELETE")
                    {
                        this.manageDelete();
                    }
        }

        public void manageGet()
        {
            String[] retrievedRequest = this.url.Split('/');

            // /users/
            if (retrievedRequest[1] == "users")
            {

            }
            // /boards/
            else
            {

            }
                // /users/[user]/invites/
                if (retrievedRequest[2] == "user")
                {

                }
                // /boards/[board]
                else
                {

                }
        }

        public void managePost()
        {
            String[] retrievedRequest = this.url.Split('/');

            // /users/
            if (retrievedRequest[1] == "users") 
            {
               // this.body[1];
            }
            // /boards/
            else 
            {
                 //this.body;
            }
                // /users/[user]/invites/
                if (retrievedRequest[2] == "user")
                {   
                   // this.body;
                }
                    // /boards/[board]
                    else
                    {
                     //   this.body;
                    }
                        
        }

        public void manageDelete()
        {
            String[] retrievedRequest = this.url.Split('/');

            // /boards/[board]
            if (retrievedRequest[1] == "board")
            {

            }
            else
            {
                this.response(this.response404);
            }
        }

        // Response 
        public StreamWriter response(String response)
        {
            sw.WriteLine(response);

            sw.Close();
            return sw;
        } 

        public void writeSuccess()
        {
            this.sw.WriteLine("HTTP/1.0 200 OK");
            this.sw.WriteLine("Connection: close");
            this.sw.WriteLine();
        }
     
    }
}
